package com.monlyu.bo;

import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;
import com.monlyu.vo.Firm;

/********************************************************************************
 * 
 * <b>功能描述☞</b><br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.bo.FirmBo.java<br>
 * <b>开发日期☞</b>2010-2-25 <br>
 * 
 * @author zengjun
 * @since JDK 5
 * @version 1.0.8
 * 
 ********************************************************************************/
@Repository(value = "firmBo")
public class FirmBo extends BaseBo {

	/**
	 * 更新联办单位信息
	 * 
	 * @param firm
	 */
	public String updateFirm(Firm firm) {
		String error = validate(firm);
		if (error == null) {
			Firm dbFirm = dao.get(Firm.class, firm.getId());
			dbFirm.setName(firm.getName());
			dbFirm.setAmount(firm.getAmount());
			dbFirm.setStartDate(firm.getStartDate());
			dbFirm.setEndDate(firm.getEndDate());
			dbFirm.setAddress(firm.getAddress());
			dbFirm.setPhone(firm.getPhone());
			dbFirm.setLinkMan(firm.getLinkMan());
			dao.update(dbFirm);
		}
		return error;
	}

	public String insertFirm(Firm firm) {
		String error = validate(firm);
		if (error == null) {
			firm.setCreateDate(DateUtil.now());
			firm.setCreateUser(WebOptUtil.curUser());
			dao.insertOrUpdate(firm);
		}
		return error;
	}

	private String validate(Firm firm) {
		if (StringUtil.isEmpty(firm.getName())) {
			return "单位名称不能为空！";
		}
		if (StringUtil.isEmpty(firm.getStartDate())) {
			return "开始时间不能为空！";
		}
		if (StringUtil.isEmpty(firm.getEndDate())) {
			return "结束时间不能为空！";
		}
		if (firm.getStartDate().after(firm.getEndDate())) {
			return "开始时间不能晚于结束时间！";
		}
		if (StringUtil.isEmpty(firm.getAmount())) {
			return "支持金额不能为空！";
		}
		return null;
	}

	public Pages<Firm> getFirms(Page page) {
		Criteria criteria = dao.initCriteria(Firm.class);
		criteria.createAlias("createUser", "createUser",
				CriteriaSpecification.LEFT_JOIN);
		if (!StringUtil.isEmpty(page.getKey())) {
			criteria.add(Restrictions.like("name", page.getKey().trim(),
					MatchMode.ANYWHERE));
		}
		criteria.addOrder(Order.desc("createDate"));
		return findByCriteria(page.getNo(), page.getSize(), criteria);
	}

	public void load(Firm firm) {
		dao.load(firm);
	}

	public Pages<Firm> search(Page page, Firm firm) {
		Criteria criteria = dao.initCriteria(Firm.class);
		criteria.createAlias("createUser", "createUser",
				CriteriaSpecification.LEFT_JOIN);
		if (!StringUtil.isEmpty(firm.getName())) {
			criteria.add(Restrictions.like("name", firm.getName().trim(),
					MatchMode.ANYWHERE));
		}
		if (firm.getStartDate() != null) {
			criteria.add(Restrictions.ge("startDate", DateUtil
					.getStartTimeOfDate(firm.getStartDate())));
		}
		if (firm.getEndDate() != null) {
			criteria.add(Restrictions.le("endDate", DateUtil
					.getEndTimeOfDate(firm.getEndDate())));
		}
		if (firm.getAmount() != null) {
			criteria.add(Restrictions.ge("amount", firm.getAmount()));
		}
		criteria.addOrder(Order.desc("createDate"));
		return findByCriteria(page.getNo(), page.getSize(), criteria);
	}

}
